#include "Geometry.fxh"

/*
Macros: Geometry.fxh plus:
TYPE = 0 (translucent), 1 (emissive)
FOG = 0, 1
*/

#if FOG != 0
	float4x4 g_WorldView;
	float2 g_FogParams;
	#if TYPE == 0 // Translucent
		float4 g_FogModulateMask;
	#elif TYPE == 1 // Emissive
		float3 g_FogColor;
	#endif
#endif


struct VS_TO_PS
{
	float4 Pos : POSITION;
	#if VERTEX_COLOR != 0
		float4 Color : COLOR0;
	#endif
	#ifdef USE_TEXCOORD
		float2 TexCoord : TEXCOORD0;
	#endif
	#if FOG != 0
		float Pos_View_z : TEXCOORD1;
	#endif
};


void MainVS(in APP_TO_VS In, out VS_TO_PS Out)
{
	float3 pos_Local = PreprocessLocalPos(In);
	
	Out.Pos = mul(float4(pos_Local, 1), g_WorldViewProj);
	#if VERTEX_COLOR != 0
		Out.Color = In.Color;
	#endif
	#ifdef USE_TEXCOORD
		Out.TexCoord = CalcTexCoord(In);
	#endif
	
	#if FOG != 0
		Out.Pos_View_z = mul(float4(pos_Local, 1), (float4x3)g_WorldView).z;
	#endif
}

void MainPS(in VS_TO_PS In, out float4 Out : COLOR0)
{
	Out = float4(1, 1, 1, 1);
	#if CONSTANT_COLOR == 1
		Out *= g_ConstantColor;
	#endif
	#if VERTEX_COLOR == 1
		Out *= In.Color;
	#endif
	#if TEXTURE == 1
		Out *= tex2D(DiffuseSampler, In.TexCoord);
	#endif
	
	#if ALPHA_TEST != 0
		clip(Out.a - g_AlphaTestRef);
	#endif
	
	#if FOG != 0
		float fogFactor = saturate(In.Pos_View_z * g_FogParams.x + g_FogParams.y);
		#if TYPE == 0 // Translucent
			fogFactor = 1 - fogFactor;
			Out *= lerp(
				float4(1, 1, 1, 1),
				float4(fogFactor, fogFactor, fogFactor, fogFactor),
				g_FogModulateMask);
		#elif TYPE == 1 // Emissive
			Out.rgb = lerp(Out.rgb, g_FogColor, fogFactor);
		#endif
	#endif
}


technique Main {
	pass {
		VertexShader = compile vs_3_0 MainVS();
		PixelShader  = compile ps_3_0 MainPS();
	}
}

